package com.cloudcc.mobile.view.main;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.cloudcc.cloudframe.util.LogUtils;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.util.UIUtils;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.view.base.BaseFragmentActivity;
import com.cloudcc.mobile.view.base.BaseWebViewFragment;

/**
 * @Description: 自定义主页
 * @author wangzhiqiang
 * @date 2015-12-28 上午11:02:02
 * 
 */
public class MainCustomActivity extends BaseFragmentActivity {

	@Override
	public void init(Bundle savedInstanceState) {
		super.init(savedInstanceState);
		Fragment fragment = new CustomFragment();
		fragment.setArguments(getIntent().getExtras());
		getSupportFragmentManager().beginTransaction()
				.add(getContainerId(), fragment).commit();
	}

	/**
	 * @Description: 自定义主页的 web展示页面
	 * @author wangzhiqiang
	 * @date 2015-12-28 上午11:12:06
	 * 
	 */
	private class CustomFragment extends BaseWebViewFragment {

		private String mCustomPage = "";
		private boolean isLoadingCustomPage;

		@Override
		public void init() {
			// 判断是从那个页面进来的
			Bundle bundle = getArguments();
			if (bundle == null) {
				return;
			}
			int flag = bundle.getInt("from.type", 1);// 类型
			mCustomPage = UrlManager.getHeaderUrl(UserManager.getManager().getUser().appMainPage);
			//mCustomPage = "/customize/page/bDTNl6221/honCal.jsp?name=honCal";
			if (flag == 2) {// 需要首先去登录 
				mUrl = UrlManager.getBindingUrl()+ RunTimeManager.getInstance().getServerBinding();
			} else {
				mUrl = UrlManager.getRootUrl()+ mCustomPage;
			}
			super.init();
		}

		@Override
		protected void onPreInitWebView() {
			// mCustomPage ="file:///android_asset/html/test.html";
			mWebView.setVisibility(View.INVISIBLE);
		}

		@Override
		protected void initClient() {
			super.initClient();
			mWebView.addJavascriptInterface(new Myjs(), "cloudcc");
		}

		public class Myjs extends JsInteration {
			@JavascriptInterface
			public void back() {
				Toast.makeText(mContext, "进入乐盟", 0).show();
			}

			@JavascriptInterface
			public void home() {
				UIUtils.enterMainUI(mContext, 1);
				MainCustomActivity.this.finish();
			}
		}

		@Override
		public WebViewClient getWebViewClient() {
			return new WebViewClient() {

				@Override
				public void onPageStarted(WebView view, String url,
						Bitmap favicon) {
					LogUtils.d("webview", "开始加载:" + url);
					startProgressDialog();
					if (isCustomPage(url)) {// 是自定义页面了
						isLoadingCustomPage = true;
					} else {
						mWebView.setVisibility(View.INVISIBLE);
					}
					super.onPageStarted(view, url, favicon);

				}

				@Override
				public void onPageFinished(WebView view, String url) {
					super.onPageFinished(view, url);
					LogUtils.d("webview", "加载完成:" + url);
					if (isLoadingCustomPage && loadProgress == 100) {
						mWebView.setVisibility(View.VISIBLE);
						stopProgressDialog();
					}
				}

				@Override
				public void onLoadResource(WebView view, String url) {
					super.onLoadResource(view, url);
				}
				
				@Override
				public boolean shouldOverrideUrlLoading(WebView view, String url) {
					// 判断是登录失败还是登录成功
					if (isLoginSuccess(url)) {// 是binding
						view.loadUrl(UrlManager.getRootUrl()+ mCustomPage);
					}
					if (isGoAppUrl(url)) {
						goApp();
						return true;
					}
					return super.shouldOverrideUrlLoading(view, url);
				}
			};
		};

		private boolean isCustomPage(String url) {
			return (!TextUtils.isEmpty(mCustomPage))
					&& StringUtils.contains(url, mCustomPage);
		}
	}
	
	/**
	 * 进入app
	 */
	private void goApp(){
		UIUtils.enterMainUI(mContext, 1);
		MainCustomActivity.this.finish();
		this.finish();
	}
	
	/** 进入app
	 * @param url
	 * @return
	 */
	private boolean isGoAppUrl(String url){
		return StringUtils.contains(url, "/goMobileHome.action");
	}

	private boolean isLoginSuccess(String url) {
		int end = url.indexOf(";");
		String name;
		if (end != -1) {
			name = url.substring(0, end);
		} else {
			name = url;
		}
		if (name.equals(UrlTools.weixinUrl + UrlTools.loginWeb)
				|| name.equals(UrlTools.weixinUrl + "/main.action")
				|| UrlManager.getManager().isHome(url)) {
			AppContext.islogining = true;
			return true;
		}
		return false;
	}

}
